package com.gewu.bookeeping.View.ItemFragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.gewu.bookeeping.IModel.IMenyInDaoModel;
import com.gewu.bookeeping.MyDb.bean.ALIBeanIn;
import com.gewu.bookeeping.MyDb.bean.ALiBeanOut;
import com.gewu.bookeeping.MyDb.bean.MesBeanOut;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanIn;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanOut;
import com.gewu.bookeeping.MyDb.dao.ALiDaoInPerson;
import com.gewu.bookeeping.MyDb.dao.ALiDaoOutPerson;
import com.gewu.bookeeping.MyDb.dao.MesOutPerson;
import com.gewu.bookeeping.R;
import com.gewu.bookeeping.Utils.MyNotiListion;
import com.gewu.bookeeping.View.activity.ItemActivity;
import com.gewu.bookeeping.View.adapter.OneItem2Adapter;

import java.util.List;

public class oneItem2Fragment extends Fragment implements IMenyInDaoModel.ICallBack2 ,com.gewu.bookeeping.OnClick.goBack{

    private ExpandableListView listView;
    private OneItem2Adapter oneItemAdapter;
    private ALiDaoOutPerson aLiDaoOutPerson;
    private ALiDaoInPerson aLiDaoInPerson;


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = LayoutInflater.from(container.getContext()).inflate(R.layout.activity_one_item2_fragment, container, false);
        initview(inflate);
        initAdapter();
        initShuJu();
        initShuJu2();
        return inflate;
    }

    /**
     * 获取支出数据
     */
    private void initShuJu() {
        List<ALiBeanOut> quer = aLiDaoOutPerson.quer();
        if (quer == null) {
            return;
        }
        for (int i = 0; i < quer.size(); i++) {
            oneItemAdapter.text1Add(quer.get(i));
        }
        oneItemAdapter.notifyDataSetChanged();
    }


    /**
     * 获取收入数据
     */
    private void initShuJu2() {
        List<ALIBeanIn> quer = aLiDaoInPerson.quer();
        if (quer == null) {
            return;
        }
        for (int i = 0; i < quer.size(); i++) {
            oneItemAdapter.text2Add(quer.get(i));
        }
        oneItemAdapter.notifyDataSetChanged();
    }


    private void initAdapter() {
        oneItemAdapter = new OneItem2Adapter(this);
        listView.setAdapter(oneItemAdapter);
    }

    /**
     * 初始化适配器
     *
     * @param inflate
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void initview(View inflate) {
        listView = inflate.findViewById(R.id.EtListView);
        aLiDaoOutPerson = new ALiDaoOutPerson(getContext());
        aLiDaoInPerson = new ALiDaoInPerson(getContext());
        MyNotiListion.getInstance2(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        initShuJu();
        initShuJu2();
    }

    @Override
    public void CallBack() {
        initShuJu();
        initShuJu2();
    }

    @Override
    public void onItemClieck(int index ,int type) {
        Intent intent = new Intent(getActivity(), ItemActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("values",index);
        bundle.putInt("type",type);
        intent.putExtra("date",bundle);
        startActivity(intent);
    }

    @Override
    public <E> void longItemClick(E e, final int groupPosition) {
        String text;
        ALIBeanIn e2 = null;
        ALiBeanOut e1 = null;
        if (groupPosition == 0) {
            e1 = (ALiBeanOut) e;
            text = e1.getText();
        } else {
            e2 = (ALIBeanIn) e;
            text = e2.getText();
        }

        final ALiBeanOut finalE = e1;
        final ALIBeanIn finalE1 = e2;


        AlertDialog alertDialog = new AlertDialog.Builder(getContext())
                .setTitle("收藏")//标题
                .setMessage("您即将收藏" + text + "这条消息！")//内容
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MesOutPerson mesOutPerson = new MesOutPerson(getContext());
                        if (groupPosition == 0) {
                            MesBeanOut mesBeanOut = new MesBeanOut();
                            mesBeanOut.setText(finalE.getText());
                            mesBeanOut.setTime(finalE.getTime());
                            mesBeanOut.setMoney(finalE.getMoney());
                            mesOutPerson.add(mesBeanOut);
                        } else {
                            MesBeanOut mesBeanOut = new MesBeanOut();
                            mesBeanOut.setText(finalE1.getText());
                            mesBeanOut.setTime(finalE1.getTime());
                            mesBeanOut.setMoney(finalE1.getMoney());
                            mesOutPerson.add(mesBeanOut);
                        }
                        dialog.dismiss();
                    }
                })
                .create();
        alertDialog.show();

    }

}